package com.boarsoft.wechat.biz.impl;

import java.util.List;

import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.dao.SimpleDao;
import com.boarsoft.wechat.biz.WeChatBiz;
import com.boarsoft.wechat.entity.WeChatTemplate;

@Transactional("wechatTransactionManager")
@Component("weChatBiz")
public class WeChatBizImpl implements WeChatBiz {

	@Autowired
	@Qualifier("wechatSimpleDao")
	protected SimpleDao dao;
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public PagedResult<WeChatTemplate> list(String orderBy, int pageNo, int pageSize) {
		StringBuilder hql = new StringBuilder();
		hql.append(" FROM ").append(WeChatTemplate.class.getName());

		Query<Number> query1 = dao.createQuery("SELECT COUNT(*) ".concat(hql.toString()));
		
		int total = ((Number) query1.uniqueResult()).intValue();

		if (Util.strIsNotEmpty(orderBy)) {
			hql.append(" ORDER BY ").append(orderBy);
		} else {
			hql.append(" ORDER BY id ");
		}

		Query<WeChatTemplate> query2 = dao.createQuery(hql.toString());
		
		query2.setFirstResult((pageNo - 1) * pageSize);
		query2.setMaxResults(pageSize);

		List<WeChatTemplate> templates = query2.list();
		for (WeChatTemplate template : templates) {
			dao.evict(template);
		}
		return new PagedResult<WeChatTemplate>(total, templates, pageNo, pageSize);
	}

	@Override
	@Transactional
	public void save(WeChatTemplate weChatTemplate) {
		// ID不为空，执行更新操作
		if (Util.strIsNotEmpty(weChatTemplate.getId())) {
//			WeChatTemplate templateNow = dao.get(WeChatTemplate.class, weChatTemplate.getId());
//			templateNow.setCode(weChatTemplate.getCode());
//			templateNow.setName(weChatTemplate.getName());
//			templateNow.setTemplateId(weChatTemplate.getTemplateId());
//			templateNow.setPlatformFakeId(weChatTemplate.getPlatformFakeId());
//			templateNow.setStatus(weChatTemplate.getStatus());
//			templateNow.setMiniProgramAppId(weChatTemplate.getMiniProgramAppId());
//			templateNow.setMiniProgramPagePath(weChatTemplate.getMiniProgramPagePath());
//			templateNow.setUrl(weChatTemplate.getUrl());
//			templateNow.setSendAccKey(weChatTemplate.getSendAccKey());
//			templateNow.setDataKey(weChatTemplate.getDataKey());
//			templateNow.setDataColor(weChatTemplate.getDataColor());
//			templateNow.setContent(weChatTemplate.getContent());
//			templateNow.setDevice(weChatTemplate.getDevice());
//			templateNow.setMemo(weChatTemplate.getMemo());
			dao.update(weChatTemplate);
		} else {
			// 增加操作
			dao.save(weChatTemplate);
		}
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(WeChatTemplate.class, id);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public WeChatTemplate get(String id) {
		StringBuilder hql = new StringBuilder();
		hql.append(" FROM ").append(WeChatTemplate.class.getName()).append("  AS template ");
		hql.append(" LEFT JOIN FETCH template.msgTypes ").append(" WHERE template.id = ? ");
		Query<WeChatTemplate> query = dao.createQuery(hql.toString());
		query.setParameter(0, id);
		List<WeChatTemplate> templates = query.list();
		if (templates != null && templates.size() > 0) {
			return templates.get(0);
		}
		return null;
	}

	public SimpleDao getDao() {
		return dao;
	}

	public void setDao(SimpleDao dao) {
		this.dao = dao;
	}
}
